package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoMountLikes;
import com.muyu.danmo.domain.DanmoMountMessage;
import com.muyu.danmo.mapper.DanmoMountLikesMapper;
import com.muyu.danmo.mapper.DanmoMountMessageMapper;
import com.muyu.danmo.service.IDanmoMountLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 装裱点赞Service业务层处理
 *
 * @author yangle
 * @date 2024-10-14
 */
@Service
public class DanmoMountLikesServiceImpl implements IDanmoMountLikesService
{
    @Autowired
    private DanmoMountLikesMapper danmoMountLikesMapper;

    /**
     * 查询装裱点赞
     *
     * @param id 装裱点赞主键
     * @return 装裱点赞
     */
    @Override
    public DanmoMountLikes selectDanmoMountLikesById(Long id)
    {
        return danmoMountLikesMapper.selectDanmoMountLikesById(id);
    }

    /**
     * 查询装裱点赞列表
     *
     * @param danmoMountLikes 装裱点赞
     * @return 装裱点赞
     */
    @Override
    public List<DanmoMountLikes> selectDanmoMountLikesList(DanmoMountLikes danmoMountLikes)
    {
        return danmoMountLikesMapper.selectDanmoMountLikesList(danmoMountLikes);
    }

    /**
     * 新增装裱点赞
     *
     * @param danmoMountLikes 装裱点赞
     * @return 结果
     */
    @Override
    public int insertDanmoMountLikes(DanmoMountLikes danmoMountLikes)
    {
        return danmoMountLikesMapper.insertDanmoMountLikes(danmoMountLikes);
    }

    /**
     * 修改装裱点赞
     *
     * @param danmoMountLikes 装裱点赞
     * @return 结果
     */
    @Override
    public int updateDanmoMountLikes(DanmoMountLikes danmoMountLikes)
    {
        return danmoMountLikesMapper.updateDanmoMountLikes(danmoMountLikes);
    }

    /**
     * 批量删除装裱点赞
     *
     * @param ids 需要删除的装裱点赞主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMountLikesByIds(Long[] ids)
    {
        return danmoMountLikesMapper.deleteDanmoMountLikesByIds(ids);
    }

    /**
     * 删除装裱点赞信息
     *
     * @param id 装裱点赞主键
     * @return 结果
     */
    @Override
    public int deleteDanmoMountLikesById(Long id)
    {
        return danmoMountLikesMapper.deleteDanmoMountLikesById(id);
    }

    @Autowired
    private DanmoMountMessageMapper danmoMountMessageMapper;
    @Override
    public Result likeMountLikes(Long userId, Long mountMessageId) {
        DanmoMountMessage danmoMountMessage = danmoMountMessageMapper.selectDanmoMountMessageById(mountMessageId);
        if (danmoMountLikesMapper.existsByUserIdAndTrainingClassId(userId, mountMessageId)) {
            return Result.success(mountMessageId,"用户已点赞");
        }

        Long amountPraise = danmoMountMessage.getAmountPraise();
        danmoMountMessage.setAmountPraise(amountPraise + 1);
        DanmoMountLikes danmoMountLikes = new DanmoMountLikes();
        danmoMountLikes.setUserId(userId);
        danmoMountLikes.setMountMessageId(mountMessageId);
        insertDanmoMountLikes(danmoMountLikes);
        danmoMountLikesMapper.updateAmountPraise(mountMessageId);
        danmoMountMessageMapper.updateDanmoMountMessage(danmoMountMessage);
        return Result.success(mountMessageId, "点赞成功");
    }
}